
#include "menu.h"

struct menu *find_menu(struct menu *p, char *menuname)
{
	while (p!=NULL)
	{
		if (strcasecmp(menuname, p->name) == 0) break;
		p = p->prev;
	}

	if (p==NULL)
	{
		dolog(LOG_DEBUG, "Undefined Menu '%s'\n", menuname);
	}

	return p;

}

struct menu *add_menu (struct menu *previous, char *name)
{
	struct menu *p;

	p = (struct menu *)malloc(sizeof(struct menu));
	p->name = strdup(name);
	p->prev = previous;

	p->back = NULL;
	p->entries = NULL;
	p->pointer = NULL;

	return p;
}

void add_menu_entry(struct menu *mymenu, char *entrylabel, void(*entryaction)(char*), char *entryargs)
{
	struct menu_entry *p, *old;

	dolog(LOG_DEBUG, "add_menu_entry: starting\n");

	p = (struct menu_entry *)malloc(sizeof(struct menu_entry));
	old = mymenu->entries;
	mymenu->entries = p;
	p->down = old;
	p->up = NULL;
	if (!(old==NULL)) {
		old->up = p;
	}

	p->label = strdup(entrylabel);
	p->action = entryaction;
	p->args = entryargs ? strdup(entryargs) : entryargs;

	mymenu->pointer = mymenu->entries;

}

void display_menu(struct menu *mymenu)
{

#define MaxStringLength 200

char line0[MaxStringLength+1];
char line1[MaxStringLength+1];
char line2[MaxStringLength+1];
char line3[MaxStringLength+1];

	struct menu_entry *p;


        snprintf(line0,MaxStringLength, "                                                                         ");
        snprintf(line1,MaxStringLength, "                                                                         ");
        snprintf(line2,MaxStringLength, "                                                                         ");
        snprintf(line3,MaxStringLength, "                                                                         ");
	p=mymenu->pointer;

	if (!(p->up==NULL)) {
		p = p->up;
	}  

	if (!(p==NULL)) {
	        snprintf(line0, MaxStringLength, " %s", p->label);
	}
	displayline(0,line0, (p==mymenu->pointer), (p==mymenu->pointer));

dolog(LOG_DEBUG,"%d line0 = %s\n", (p==mymenu->pointer), line0);

	if (!(p==NULL)) {
		p = p->down;
	        if (!(p==NULL)) snprintf(line1, MaxStringLength, " %s", p->label);
	}

dolog(LOG_DEBUG,"%d line1 = %s\n", (p==mymenu->pointer), line1);

	displayline(1,line1, (p==mymenu->pointer), (p==mymenu->pointer));

	if (!(p==NULL)) {
		p = p->down;
	        if (!(p==NULL)) snprintf(line2, MaxStringLength, " %s", p->label);
	}
	displayline(2,line2, (p==mymenu->pointer), (p==mymenu->pointer));

dolog(LOG_DEBUG,"%d line2 = %s\n", (p==mymenu->pointer), line2);

	if (!(p==NULL)) {
		p = p->down;
	        if (!(p==NULL)) snprintf(line3, MaxStringLength, " %s", p->label);
	}
	displayline(3,line3, (p==mymenu->pointer), (p==mymenu->pointer));

dolog(LOG_DEBUG,"%d line3 = %s\n", (p==mymenu->pointer), line3);

}


bool extract_menu_entry(struct menu *p, char *buf, struct flabbybox_funcs *func_map)
{

	unsigned int i, o = 0, len = strlen(buf), l, func = 0;
	char *arg = NULL;
	char *label = NULL;

	dolog(LOG_DEBUG, "extract_menu_entry: buf is '%s'\n", buf);

	l = strcspn(buf, " "); /*first 'l' chars are the label*/
	dolog(LOG_DEBUG, "extract_menu_entry: l is '%d'\n", l);
	buf[l] = '\0';
	o += l+1;

	dolog(LOG_DEBUG, "extract_menu_entry: label is '%s'\n", buf);
	dolog(LOG_DEBUG, "extract_menu_entry: left to check is '%s'\n", &buf[o]);

	for (i=0; func_map[i].name != NULL; i++)
	{
		l = strlen(func_map[i].name);
		dolog(LOG_DEBUG, "extract_menu_entry: try function '%s'\n", func_map[i].name);
		if (len != o+l && (len < o+l || buf[o+l] != ' ')) continue;
		if (strncasecmp(&buf[o], func_map[i].name, l) == 0) break;
	}

	if (func_map[i].name == NULL)
	{
		dolog(LOG_DEBUG, "Undefined Function '%s'\n", &buf[o]);
		return false;
	}
	func = i;

	dolog(LOG_DEBUG, "extract_menu_entry: func is '%s'\n", func_map[i].name);


	o += l+1;
	if (len > o) arg = &buf[o];

	dolog(LOG_DEBUG, "extract_menu_entry: arg is '%s'\n", arg);

	add_menu_entry(p, buf, func_map[func].function, arg);

	return true;

}

